// 订单提交页面js
function getQueryString(key) {
    var reg = new RegExp("(^|&)" + key + "=([^&]*)(&|$)");
    var result = window.location.search.substr(1).match(reg);
    return result ? decodeURIComponent(result[2]) : null;
}

layui.use(['form', 'layedit', 'laydate'], function () {
    var form = layui.form
        , layer = layui.layer
        , layedit = layui.layedit
        , laydate = layui.laydate;
});
$(function () {
    var userName = $("#userid").val();
    var id = getQueryString("id");
    var buySum = getQueryString("buySum");

    $.ajax({
        url: "/order/getProById",
        type: "GET",
        dataType: "json",
        data: {id:id,buySum:buySum},
        success: function (res) {
            if (res.code === 0) {
                var orderList = res.data;
                var str = "";
                var str1 = "";
                for (var i = 0; i < orderList.length; i++) {
                    str += `
                    <dd class="item clearfix">
                    <div class="item-row">
                        <div class="col col-1">
                            <div class="g-pic">
                                <img src="http://i1.mifile.cn/a1/T11lLgB5YT1RXrhCrK!40x40.jpg"
                                    srcset="http://i1.mifile.cn/a1/T11lLgB5YT1RXrhCrK!80x80.jpg 2x"
                                    width="50" height="50" />
                            </div>
                            <div class="g-info">
                                <a href="#">
                                    ${orderList[i].product_name}
                                </a>
                            </div>
                        </div>

                        <div class="col col-2">${orderList[i].product_price}元</div>
                        <div class="col col-3">${orderList[i].buy_sum}</div>
                        <div class="col col-4">${orderList[i].price_sum}元</div>
                    </div>
                </dd>
                    `;
                }
                $("#orderList").append(str);
            }
        }
    });
    $("#J_editAddrOk").click(function () {
        //地址
        var consignee = $(".selected .itemConsignee").text();
        var tag = $(".selected .itemTag").text();
        var tel = $(".selected .itemTel").text();
        var region = $(".selected .itemRegion").text();
        var street = $(".selected .itemStreet").text();
        //支付方式
        var pay_way = $("#pay_way").text().trim();
        //配送方式
        var delivery_way = $("#delivery_way").text().trim();
        var dataAd = {
            "consignee": consignee, "tag": tag, "tel": tel, "region": region, "street": street,
            "pay_way": pay_way, "delivery_way": delivery_way, "userId": $("#userid").val()
        };
        $.ajax({
            url: "/order/saveAddress",
            type: "post",
            dataType: "json",
            data: JSON.stringify(dataAd),
            success: function (res) {
                if (res.code === 0) {
                    layer.alert("保存成功！", {
                        icon: 2,
                        skin: 'layer-ext-moon' //该皮肤由layer.seaning.com友情扩展。关于皮肤的扩展规则，去这里查阅
                    });
                }
            }
        });
    });
    //地址修改
    $("#bianji").click(function () {
        //地址
        var consignee = $(".selected .itemConsignee").text();
        var tag = $(".selected .itemTag").text();
        var tel = $(".selected .itemTel").text();
        var region = $(".selected .itemRegion").text();
        var street = $(".selected .itemStreet").text();
        //支付方式
        var pay_way = $("#pay_way").text().trim();
        //配送方式d
        var delivery_way = $("#delivery_way").text().trim();
        var dataAdd = {
            "consignee": consignee, "tag": tag, "tel": tel, "region": region, "street": street,
            "pay_way": pay_way, "delivery_way": delivery_way, "userId": $("#userid").val()
        };
        $.ajax({
            url: "/order/updateAddress",
            type: "post",
            dataType: "json",
            data: JSON.stringify(dataAdd),
            success: function (res) {
                if (res.code === 0) {
                    layer.alert("修改成功！", {
                        icon: 2,
                        skin: 'layer-ext-moon' //该皮肤由layer.seaning.com友情扩展。关于皮肤的扩展规则，去这里查阅
                    });
                }
            }
        });
    });

});